//
//  MBAutoFontSizeLabel.m
//  mZero-iOS
//
//  Created by catch on 16/6/13.
//  Copyright © 2016年 Shenzhen Maker Works Technology Co., Ltd. All rights reserved.
//

#import "MBAutoFontSizeLabel.h"
#import <QuartzCore/QuartzCore.h>

@interface MBAutoFontSizeLabel()

//原始的font
@property (nonatomic, strong) UIFont *oriFont;

@end

@implementation MBAutoFontSizeLabel

#pragma maek -- init

-(instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self commonInit];
    }
    return self;
}

-(void)commonInit{
    _oriFont = self.font;
    _autoAdjustFontSizeWithTextChange = YES;
    if (_localizedString) {
        self.text = NSLocalizedString(_localizedString,nil);
    }
    [self _setFontSizeThatFits];
}

-(void)setLocalizedString:(NSString *)localizedString{
    _localizedString = localizedString;
    if (localizedString) {
        self.text = NSLocalizedString(_localizedString,nil);
        [self setNeedsDisplay];
    }
}

#pragma maek -- public methods

- (void)setEnlargedSize:(CGSize)enlargedSize{
    if (!CGSizeEqualToSize(enlargedSize, _enlargedSize)){
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        
        _enlargedSize = enlargedSize;
        CGRect oldFrame = self.frame;
        
        self.transform = CGAffineTransformIdentity;
        super.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, enlargedSize.width, enlargedSize.height);
        [self _setFontSizeThatFits];
        
        if (!CGRectEqualToRect(oldFrame, CGRectZero))
            self.frame = oldFrame;
        
        [CATransaction commit];
    }
}

#pragma maek -- override methods

- (void)setText:(NSString *)text{
    if (self.text == nil || _autoAdjustFontSizeWithTextChange){
        super.text = text;
        [self _setFontSizeThatFits];
    }else{
        super.text = text;
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self _setFontSizeThatFits];
}


#pragma maek -- setters

-(void)setAutoAdjustFontSizeWithTextChange:(BOOL)autoAdjustFontSizeWithTextChange{
    _autoAdjustFontSizeWithTextChange = autoAdjustFontSizeWithTextChange;
    if (_autoAdjustFontSizeWithTextChange) {
        [self setNeedsDisplay];
    }else{
        super.font = _oriFont;
    }
}

#pragma maek -- private methods

/**
 *  设置合适的字体大小
 */
- (void)_setFontSizeThatFits{
    CGFloat maxFontSizeCorrection;
    if (self.bounds.size.height > 8.0){
        maxFontSizeCorrection = 4.0;
    }else{
        maxFontSizeCorrection = self.bounds.size.height > 4.0 ? self.bounds.size.height - 4.0 : 0.0;
    }
    CGFloat maxFontSize = self.bounds.size.height - maxFontSizeCorrection;
    NSString *templateText = (_textForFontSizeCalculation == nil) ? self.text : _textForFontSizeCalculation;
    
    CGFloat  fitSize = [self _getFontForString:templateText toFitInRect:self.frame seedFontSize:maxFontSize];
    super.font = [self.font fontWithSize:fitSize];
}

/**
 *  获取适合的字体大小
 *
 *  @param string       文字
 *  @param rect         大小
 *  @param seedFontSize 默认字体
 *
 *  @return 适合的字体大小
 */
-(CGFloat)_getFontForString:(NSString*)string
                toFitInRect:(CGRect)rect
               seedFontSize:(CGFloat)seedFontSize{
    CGFloat returnSize = seedFontSize;
    UIFont* returnFont = [self.font fontWithSize:seedFontSize];
    CGSize stringSize = [string sizeWithAttributes:@{NSFontAttributeName : returnFont}];
    
    int maxTime = 50;//最多寻找次数
    while(stringSize.width > rect.size.width){
        returnSize = returnFont.pointSize -1;
        returnFont = [self.font fontWithSize:returnSize];
        stringSize = [string sizeWithAttributes:@{NSFontAttributeName : returnFont}];
        maxTime --;
        if (maxTime < 1) {
            break;
        }
    }
    return returnSize;
}

@end
